package com.iplant.aps.serviceimpl;

import com.iplant.aps.service.QMSService;
import com.iplant.aps.service.po.APIResult;
import com.iplant.aps.service.po.aps.APSMaterialReturn;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.ipt.IPTOutsourcingMaterial;
import com.iplant.aps.service.po.ipt.IPTPreCheckProblem;
import com.iplant.aps.service.po.wms.WMSTechMaterialReturn;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RemoteInvokeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QMSServiceImpl implements QMSService {
    private static final Logger logger = LoggerFactory.getLogger(QMSServiceImpl.class);

    private static QMSService Instance;

    public static QMSService getInstance() {
        if (Instance == null)
            Instance = new QMSServiceImpl();
        return Instance;
    }

    public APIResult RSM_QueryTurnOrderTaskList(BMSEmployee wLoginUser, int wOrderID, int wApplyStationID,
                                                int wTargetStationID, List<Integer> wStateIDList) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("OrderID", wOrderID);
            wParms.put("ApplyStationID", wApplyStationID);
            wParms.put("TargetStationID", wTargetStationID);
            wParms.put("StateIDList", wStateIDList);
            String wUri = StringUtils.Format("api/RSMTurnOrderTask/All?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_QueryPreCheckProblemByID(BMSEmployee wLoginUser, int wProblemID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("ID", wProblemID);
            String wUri = StringUtils.Format("api/IPTPreCheckProblem/Info?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_UpdatePreCheckProblem(BMSEmployee wLoginUser, IPTPreCheckProblem wIPTPreCheckProblem) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("data", wIPTPreCheckProblem);
            String wUri = StringUtils.Format("api/IPTPreCheckProblem/Update?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_QueryPreCheckReportList(BMSEmployee wLoginUser, int wOrderID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("OrderID", wOrderID);
            String wUri = StringUtils.Format("api/IPTPreCheckReport/All?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult SFC_QueryTaskIPTList(BMSEmployee wLoginUser, int wOrderID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("OrderID", wOrderID);
            String wUri = StringUtils.Format("api/SFCTaskIPT/All?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_QueryValueList(BMSEmployee wLoginUser, int wSFCTaskIPTID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("TaskID", wSFCTaskIPTID);
            String wUri = StringUtils.Format("api/IPTStandard/IPTValue?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_QueryStandardCurrent(BMSEmployee wLoginUser, int wITPMode, int wLineID, int wPartID,
                                              int wPartPointID, String wProductNo, int wCustomID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("ITPMode", wITPMode);
            wParms.put("LineID", wLineID);
            wParms.put("PartID", wPartID);
            wParms.put("PartPointID", wPartPointID);
            wParms.put("ProductNo", wProductNo);
            wParms.put("CustomID", wCustomID);

            String wUri = StringUtils.Format("api/IPTStandard/Current?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    public APIResult IPT_QueryItemList(BMSEmployee wLoginUser, int wStandardID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("StandardID", wStandardID);

            String wUri = StringUtils.Format("api/IPTStandard/ItemListByStandard?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult RSM_QueryBOPDoneList(BMSEmployee wLoginUser, int wOrderID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("OrderID", wOrderID);

            String wUri = StringUtils.Format("api/RSMTurnOrderTask/BOPDoneList?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult SFC_QueryChangeOrderID(BMSEmployee wLoginUser, int wOrderID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("OrderID", wOrderID);

            String wUri = StringUtils.Format("api/SFCBogiesChangeBPM/QueryChangeOrderID?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult APS_UpdateMaterialReturn(BMSEmployee wLoginUser, APSMaterialReturn wAPSMaterialReturn) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();
            wParms.put("data", wAPSMaterialReturn);
            String wUri = StringUtils.Format("api/APSMaterialReturn/Update?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult IPT_OutsourcingMaterialInfo(BMSEmployee wLoginUser, int wID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("ID", wID);

            String wUri = StringUtils.Format("api/IPTOutsourcingMaterial/Info?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult IPT_OutsourcingMaterialUpdateData(BMSEmployee wLoginUser,
                                                       IPTOutsourcingMaterial wIPTOutsourcingMaterial) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("data", wIPTOutsourcingMaterial);

            String wUri = StringUtils.Format("api/IPTOutsourcingMaterial/UpdateData?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult SFC_QueryTaskIPTListByTaskStepIDList(BMSEmployee wLoginUser, List<Integer> wTaskStepIDList) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("data", wTaskStepIDList);

            String wUri = StringUtils.Format("api/SFCTaskIPT/QueryTaskListByTaskStepIDList?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult WMS_WMS_UpdateWMSTechMaterialReturn(BMSEmployee wLoginUser,
                                                         WMSTechMaterialReturn wWMSTechMaterialReturn) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("data", wWMSTechMaterialReturn);

            String wUri = StringUtils.Format("api/WMSTechMaterialReturn/Update?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult IPT_QueryMultiMaterialID(BMSEmployee wLoginUser, int routeID, int wLineID, int wProductID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("RouteID", routeID);
            wParms.put("LineID", wLineID);
            wParms.put("ProductID", wProductID);

            String wUri = StringUtils.Format("api/IPTItemMaterial/QueryMultiMaterialID?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult SFC_CreateOutMaterialTrackLedgerList(BMSEmployee wLoginUser,
                                                          IPTOutsourcingMaterial wIPTOutsourcingMaterial) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("IPTOutsourcingMaterial", wIPTOutsourcingMaterial);

            String wUri = StringUtils.Format("api/SFCOutMaterialTrackLedger/CreateOutMaterialTrackLedger?cadv_ao={0" +
                            "}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.POST);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult TCM_QueryTechChangeNotice(BMSEmployee wLoginUser, int wTechChangeNoticeID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("ID", wTechChangeNoticeID);

            String wUri = StringUtils.Format("api/TCMTechChangeNotice/Info?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public APIResult TCM_QueryMaterialChangeLog(BMSEmployee wLoginUser, int changeLogID) {
        APIResult wResult = new APIResult();
        try {
            Map<String, Object> wParms = new HashMap<>();

            wParms.put("ID", changeLogID);

            String wUri = StringUtils.Format("api/TCMMaterialChangeLog/Info?cadv_ao={0}&cade_po={1}",
                    wLoginUser.LoginName, wLoginUser.Password);
            wResult = RemoteInvokeUtils.getInstance().HttpInvokeAPI(ServerUrl, ServerName, wUri, wParms,
                    HttpMethod.GET);
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return wResult;
    }
}
